package com.hjh.netty.elect.demo5.parser.impl;

import com.hjh.netty.elect.demo5.bean.Main1Sub1Ver1;
import com.hjh.netty.elect.demo5.bean.Pmc350DevBean;
import com.hjh.netty.elect.demo5.bean.Pmc350DevUpLoadData;
import com.hjh.netty.elect.demo5.parser.UpLoadData;
import com.hjh.netty.elect.util.ModbusTools;
import io.netty.buffer.ByteBuf;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class UpLoadDataMain1Sub1Ver1 implements UpLoadData {

    @Override
    public Pmc350DevUpLoadData parserUpLoadData(ByteBuf buffer, Pmc350DevBean pmc350DevBean) {
        int totalLen = buffer.readableBytes();
        log.info("parserUpLoadData|readableBytes:{}", totalLen);
        Main1Sub1Ver1 main1Sub1Ver1 = new Main1Sub1Ver1();

        int index = 1;
        main1Sub1Ver1.setU1(buffer.readFloat());
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setU2(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setU3(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setUavg(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setU12(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setU23(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setU31(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);


        main1Sub1Ver1.setUllavg(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setI1(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setI2(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);


        main1Sub1Ver1.setI3(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setIavb(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setPa(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setPb(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setPc(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setP(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setQ1(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setQ2(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setQ3(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setQ(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setS1(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setS2(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setS3(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setS(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setPF1(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setPF2(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setPF3(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setPFavg(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setFreq(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setU1Ang(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setU2Ang(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setU3Ang(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setIaAng(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setIbAng(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setIcAng(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setIn(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setIR(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setDPF1(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setDPF2(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setDPF3(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setDPF(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        //82 预留 float
        buffer.readByte();
        buffer.readByte();
        buffer.readByte();
        buffer.readByte();
        index++;
        printLen(buffer,totalLen-4*index);

        //    84 预留 float
        buffer.readByte();
        buffer.readByte();
        buffer.readByte();
        buffer.readByte();
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setTC1(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setTC2(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setTC3(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setTC4(buffer.readFloat());
        index++;
        printLen(buffer,totalLen-4*index);


        main1Sub1Ver1.setIRalarmStatus(buffer.readUnsignedShort());

        main1Sub1Ver1.setTCalarmStatus(buffer.readUnsignedShort());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setDIStatus(buffer.readUnsignedShort());

        main1Sub1Ver1.setDILinkStatus(buffer.readUnsignedShort());

        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setDOStatus(buffer.readUnsignedShort());

        main1Sub1Ver1.setIalarmStatus(buffer.readUnsignedShort());
        index++;
        printLen(buffer,totalLen-4*index);


        main1Sub1Ver1.setSetpointStatus(buffer.readUnsignedShort());


        main1Sub1Ver1.setSelfCheckStatus(buffer.readUnsignedShort());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setSOEpointer(buffer.readInt());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setOperatingTime(buffer.readInt());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setIRCheckStatus(buffer.readUnsignedShort());
        main1Sub1Ver1.setTCCheckStatus(buffer.readUnsignedShort());
        index++;
        printLen(buffer,totalLen-4*index);

        main1Sub1Ver1.setAllalarmStatus(buffer.readUnsignedShort());
        printLen(buffer,totalLen-4*index-2);

        log.info("|parserUpLoadData|totalLen:{}|index:{}",totalLen,index);

        printDIStatus(main1Sub1Ver1);
        return main1Sub1Ver1;
    }

    /**
     * 打印DIStatus
     * @param main1Sub1Ver1
     */
    private void printDIStatus(Main1Sub1Ver1 main1Sub1Ver1 ){
        Integer diStatus = main1Sub1Ver1.getDIStatus();

        String byteInfo = "";
        while (diStatus != null && diStatus > 0){
            byteInfo = (diStatus % 2) + byteInfo;
            diStatus /= 2;
        }

            log.info("printDIStatus|diStatus:{}|value:{}",diStatus,byteInfo);
    }

    private void printLen(ByteBuf buffer,int len){
        int readableBytes = buffer.readableBytes();
        log.info("printLen|{}|len:{}|{}", readableBytes,len,readableBytes == len);
    }
}
